package folderLogger.GUI;

//Imports are listed in full to show what's being used
//could just import javax.swing.* and java.awt.* etc..
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import folderLogger.OperationSettings;
import folderLogger.CoverImageMod.ImageReferenceType;
import folderLogger.GUI.Delegates.CallbackDelegate;
import folderLogger.GUI.Delegates.SettingsDelegate;
import folderLogger.fileFilters.ExtensionFilterType;
import folderLogger.outputFile.OutputFileFormat;

public class FolderLoggerGUI {
	
	private OutputFileFormat[] formatsSupported;
	private ImageReferenceType[] imageReferenceSupported;
	private ExtensionFilterType[] extensionFiltersSupported;
	
	private JPanel panelSettings;
	private JPanel panelCenter;
	private JButton runButton;
	private JFrame guiFrame;
	
	private JComboBox<OutputFileFormat> formatSelection;
	private JComboBox<ImageReferenceType> imageCopySelection;
	private JComboBox<ExtensionFilterType> extensionFilterSelection;
	private JTextField panelRootFolder;
	private JTextField panelOutputFolder;
	private JTextField panelExtensions;
	private JTextArea outputText;
	
	private JLabel labelFormat;
	private JLabel labelImageCopy;
	private JLabel labelRootFolder;
	private JLabel labelOutputFolder;
	private JLabel labelFilterType;
	private JLabel labelExtensions;
	private JLabel labelOutputWindow;
	
	private RunButtonActionListener myListener;

	public FolderLoggerGUI()
	{
		int frameWidth = 300;
		
		guiFrame = new JFrame();
      
		//make sure the program exits when the frame closes
		guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		guiFrame.setTitle("Folder Export");
		guiFrame.setSize(frameWidth,450);
		guiFrame.setResizable(false);
	
		//This will centre the JFrame in the middle of the screen
		guiFrame.setLocationRelativeTo(null);
	  
		//Options for the JComboBox 
	  
		formatsSupported = OutputFileFormat.values();
		imageReferenceSupported = ImageReferenceType.values();
		extensionFiltersSupported = ExtensionFilterType.values();

	  
		labelFormat = new JLabel(" Format: ");
		labelRootFolder = new JLabel(" Input: ");
		labelOutputFolder = new JLabel(" Output: ");
		labelImageCopy = new JLabel(" Image: ");
		labelFilterType = new JLabel(" Filter: ");
		labelExtensions = new JLabel(" Extensions (;): ");
		  
		formatSelection = new JComboBox<OutputFileFormat>(formatsSupported);
		imageCopySelection = new JComboBox<ImageReferenceType>(imageReferenceSupported);
		extensionFilterSelection = new JComboBox<ExtensionFilterType>(extensionFiltersSupported);
		panelOutputFolder = new JTextField("");
		panelRootFolder = new JTextField("");
		panelExtensions = new JTextField("");
		
		extensionFilterSelection.setSelectedIndex(2);
		panelExtensions.setText("mp3;mpc;ogg;wav;flac");
		panelOutputFolder.setText("D:\\FolderLogger");
		
		panelSettings = new JPanel();
		panelSettings.setLayout(new GridLayout(6, 2));
		  
		panelSettings.add(labelFormat);
		panelSettings.add(formatSelection);
		panelSettings.add(labelRootFolder);
		panelSettings.add(panelRootFolder);
		panelSettings.add(labelOutputFolder);
		panelSettings.add(panelOutputFolder);
		panelSettings.add(labelImageCopy);
		panelSettings.add(imageCopySelection);
		panelSettings.add(labelFilterType);
		panelSettings.add(extensionFilterSelection);
		panelSettings.add(labelExtensions);
		panelSettings.add(panelExtensions);
	  
		panelCenter = new JPanel();
		labelOutputWindow = new JLabel("Output:");
	  
		outputText = new JTextArea();
		outputText.setPreferredSize(new Dimension(frameWidth-30, 200));
		outputText.setEditable(false);
		outputText.setBorder(BorderFactory.createLineBorder(Color.BLACK));

		panelCenter.add(labelOutputWindow);
		panelCenter.add(outputText);
	  
		runButton = new JButton("Run");
	  
		myListener = new RunButtonActionListener();
		myListener.setSettingsDelegate(new GetSettingsDelegate());
		myListener.setCallbackDelegate(new CallbackDelegate() {
			
			@Override
			public void ExecuteWhenDone(boolean isDone) {
				runButton.setEnabled(isDone);
				if(isDone) {
					String lastError = myListener.getLastError();
					outputText.setText(lastError == null ? "DONE!" : lastError);
				}
				else {
					outputText.setText("Running...");
				}
			}
		});
		
		runButton.addActionListener(myListener);
		
		guiFrame.add(panelSettings, BorderLayout.NORTH);
		guiFrame.add(panelCenter, BorderLayout.CENTER);
		guiFrame.add(runButton,BorderLayout.SOUTH);
  
		//make sure the JFrame is visible
		guiFrame.setVisible(true);
	}
  
	public class GetSettingsDelegate implements SettingsDelegate
	{
		@Override
		public OperationSettings getSettings() {
			if(extensionFilterSelection.getSelectedIndex() == -1)
			{
				//	ALERT
				return null;
			}
			ExtensionFilterType filterType = ExtensionFilterType.valueOf(extensionFilterSelection.getSelectedItem().toString());
			
			String[] filterExtensions = panelExtensions.getText().split(";");
			
			if(imageCopySelection.getSelectedIndex() == -1)
			{
				//	ALERT
				return null;
			}
			ImageReferenceType imageRefType = ImageReferenceType.valueOf(imageCopySelection.getSelectedItem().toString());
			
			String rootFolderPath = panelRootFolder.getText();
			String outputFolder = panelOutputFolder.getText();
			
			if(formatSelection.getSelectedIndex() == -1)
			{
				//	ALERT
				return null;
			}
			OutputFileFormat outputFileFormat = OutputFileFormat.valueOf(formatSelection.getSelectedItem().toString());
			
			OperationSettings settings = new OperationSettings(filterType, filterExtensions, imageRefType, rootFolderPath, outputFolder, outputFileFormat);
			
			return settings;
		}
		
	}

}